package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.MobileUser;
import com.ruoyi.business.domain.MobileUserCollection;
import com.ruoyi.business.domain.MobileUserCoupon;
import com.ruoyi.business.mapper.MobileUserCollectionMapper;
import com.ruoyi.business.mapper.MobileUserCouponMapper;
import com.ruoyi.business.mapper.MobileUserMapper;
import com.ruoyi.business.service.IMobileUserService;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.MessageException;
import com.ruoyi.common.utils.CommonUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 小程序用户Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-13
 */
@Service
public class MobileUserServiceImpl extends ServiceImpl<MobileUserMapper, MobileUser> implements IMobileUserService {

    @Autowired
    private MobileUserMapper mobileUserMapper;
    @Autowired
    private MobileUserCouponMapper couponMapper;
    @Autowired
    private MobileUserCollectionMapper collectionMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 查询小程序用户
     *
     * @param id 小程序用户主键
     * @return 小程序用户
     */
    @Override
    public MobileUser selectMobileUserById(Long id) {
        return mobileUserMapper.selectMobileUserById(id);
    }

    /**
     * 查询小程序用户列表
     *
     * @param mobileUser 小程序用户
     * @return 小程序用户
     */
    @Override
    public List<MobileUser> selectMobileUserList(MobileUser mobileUser) {
        return mobileUserMapper.selectMobileUserList(mobileUser);
    }

    /**
     * 新增小程序用户
     *
     * @param mobileUser 小程序用户
     * @return 结果
     */
    @Override
    public int insertMobileUser(MobileUser mobileUser) {
                mobileUser.setCreateTime(DateUtils.getNowDate());
            return mobileUserMapper.insertMobileUser(mobileUser);
    }

    /**
     * 修改小程序用户
     *
     * @param mobileUser 小程序用户
     * @return 结果
     */
    @Override
    public int updateMobileUser(MobileUser mobileUser) {
                mobileUser.setUpdateTime(DateUtils.getNowDate());
        return mobileUserMapper.updateMobileUser(mobileUser);
    }

    /**
     * 批量删除小程序用户
     *
     * @param ids 需要删除的小程序用户主键
     * @return 结果
     */
    @Override
    public int deleteMobileUserByIds(Long[] ids) {
        return mobileUserMapper.deleteMobileUserByIds(ids);
    }

    /**
     * 删除小程序用户信息
     *
     * @param id 小程序用户主键
     * @return 结果
     */
    @Override
    public int deleteMobileUserById(Long id) {
        return mobileUserMapper.deleteMobileUserById(id);
    }

    /**
     * @param user
     * @return java.lang.Boolean
     * @Author liumaotong
     * @Description 用户登录
     * @Date 2024/6/13
     * @Param [user]
     */
    @Override
    public MobileUser userLogin(MobileUser user) {
        if(!CommonUtils.isMobile(user.getPhone())){
            throw new MessageException("手机号填写错误！");
        }
        //查询用户是否登录过
        QueryWrapper<MobileUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MobileUser::getPhone,user.getPhone());
        MobileUser mobileUser = mobileUserMapper.selectOne(queryWrapper);
        if(null != mobileUser){
            user.setId(mobileUser.getId());
        }
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        this.saveOrUpdate(user);
        //核销员标识
        this.isVerifier(user);
        return user;
    }

    /**
     * @param user
     * @return java.lang.Boolean
     * @Author liumaotong
     * @Description 是否需要登录,返回true需要登录  返回false 不需要登录
     * @Date 2024/6/13
     * @Param [user]
     */
    @Override
    public Map<String,Object> isLogin(MobileUser user) {
        Map<String,Object> resMap = new HashMap<>(2);
        resMap.put("isLogin",false);
        if(StringUtils.isBlank(user.getOpenId())){
            throw new MessageException("openId不能为空");
        }
        //根据openId查询该用户是否登录过
        QueryWrapper<MobileUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MobileUser::getOpenId,user.getOpenId());
        MobileUser mobileUser = mobileUserMapper.selectOne(queryWrapper);
        if(null != mobileUser){
            //核销员标识
            this.isVerifier(mobileUser);

            resMap.put("isLogin",true);
            resMap.put("user",mobileUser);
        }
        return resMap;
    }

    /**
     * @param openId
     * @return com.ruoyi.business.domain.MobileUser
     * @Author liumaotong
     * @Description 小程序端用户详情
     * @Date 2024/6/14
     * @Param [openId]
     */
    @Override
    public MobileUser getUserByOpenId(String openId) {
        QueryWrapper<MobileUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MobileUser::getOpenId,openId);
        MobileUser user = mobileUserMapper.selectOne(queryWrapper);
        if(null == user){
            throw new MessageException("未查询到相关用户");
        }
        //查询优惠券状态
        QueryWrapper<MobileUserCoupon> couponQueryWrapper = new QueryWrapper<>();
        couponQueryWrapper.lambda().eq(MobileUserCoupon::getUserId,user.getId());
        MobileUserCoupon coupon = couponMapper.selectOne(couponQueryWrapper);
        user.setConponStatus(coupon.getStatus());
        //查询收藏数
        QueryWrapper<MobileUserCollection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.lambda().eq(MobileUserCollection::getOpenId,openId);
        Integer count = collectionMapper.selectCount(collectionQueryWrapper);
        user.setCollectionCount(String.valueOf(count));
        return user;
    }

    /**
     * @Author liumaotong
     * @Description 是否是核销员
     * @Date 2024/6/20
     * @Param [user]
     * @return void
     */
    private void isVerifier(MobileUser user){
        //查询该用户是否是核销员
        List<SysRole> roleList = roleMapper.selectRoleListByPhone(user.getPhone());
        if(roleList.stream().filter(r -> "verifier".equals(r.getRoleKey())).collect(Collectors.toList()).size() > 0){
            user.setIsVerifier("1");
        }else {
            user.setIsVerifier("0");
        }
    }
}